package com.rcpcompany.ui.flows;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPartSite;

/**
 * Abstract {@link IFlowPage} that must be used as the super-class for all
 * 
 * @author Tonny Madsen, The RCP Company
 */
public abstract class AbstractFlowPage extends WizardPage implements IFlowPage {

	/**
	 * The flow director of this page.
	 */
	private IFlowDirector myFlowDirector;
	/**
	 * The flow container of this page.
	 */
	private IFlowContainer myFlowContainer;

	public AbstractFlowPage(String title, ImageDescriptor titleImage) {
		super("xx", title, titleImage); //$NON-NLS-1$
	}

	public abstract void createControl(Composite parent);

	public void initialize(IFlowDirector director, IFlowContainer container) {
		myFlowDirector = director;
		myFlowContainer = container;
	}

	@Override
	public void dispose() {
		myFlowDirector = null;
		myFlowContainer = null;
		super.dispose();
	}

	public IFlowDirector getFlowDirector() {
		return myFlowDirector;
	}

	public IFlowContainer getFlowContainer() {
		return myFlowContainer;
	}

	public void pageUpdated() {
		if (getFlowContainer() != null) {
			getFlowContainer().pageUpdated(this);
		}
	}

	public abstract void refreshState();

	public abstract void saveState();

	@Override
	public boolean isCurrentPage() {
		return getFlowContainer().getCurrentPage() == this;
	}

	// Null out some of the methods from DialogPage and WizardPage

	@Override
	public void setPageComplete(boolean complete) {
	}

	@Override
	public boolean isPageComplete() {
		return false;
	}

	@Override
	public final void setVisible(boolean visible) {
	}

	@Override
	public final boolean canFlipToNextPage() {
		return false;
	}

	@Override
	protected final IWizardContainer getContainer() {
		return null;
	}

	@Override
	public final IWizard getWizard() {
		return null;
	}

	@Override
	public final void setPreviousPage(IWizardPage page) {
	}

	@Override
	public final IWizardPage getPreviousPage() {
		return null;
	}

	@Override
	public final void setWizard(IWizard newWizard) {
	}

	private SelectionListener mySelectionListener = new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent e) {
			pageUpdated();
		}
	};

	/**
	 * Returns a common {@link SelectionListener} that can be used with all
	 * structured widgets ({@link Table}, {@link Combo}, etc.).
	 * 
	 * @return the listener
	 */
	public SelectionListener getPageUpdatedSelectionListener() {
		return mySelectionListener;
	}

	private ModifyListener myModifyListener = new ModifyListener() {
		public void modifyText(ModifyEvent e) {
			pageUpdated();
		}
	};

	/**
	 * Returns a common {@link ModifyListener} that can be used with all
	 * {@link Text} widgets.
	 * 
	 * @return the listener
	 */
	public ModifyListener getPageUpdatedModifyListener() {
		return myModifyListener;
	}

	@Override
	public String toString() {
		return getClass().getName() + "[" + getTitle() + "]" + "@" + Integer.toHexString(hashCode()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

	public IWorkbenchPartSite getSite() {
		return getFlowContainer().getSite();
	}
}
